#!/usr/bin/env bash
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.

# Deregister code from the alternatives system
update-alternatives --remove editor /usr/bin/@@NAME@@

# Uninstall desktop icon
function ky_prerm_uninstall_ide_desktop_icon(){
    gDesktopName=""
	if [ -d "/root/桌面" ]; then
		gDesktopName="桌面"
	elif [ -d "/root/Desktop" ]; then
		gDesktopName="Desktop"
	fi

    if [ ! -z "$gDesktopName" ]; then
        if [ -d "/root/${gDesktopName}" ]; then
        	rm -f /root/${gDesktopName}/@@NAME@@.desktop
        fi
    fi

    for FILENAME in /home/*; do
        USERNAME="${FILENAME##*/}"
        [ ! -d "${FILENAME}" ] && continue
        getent passwd "${USERNAME}" >/dev/null 2>&1 || continue

        if id -u `echo ${FILENAME} | awk '{print substr($FILENAME, 7, 32)}'` >/dev/null 2>&1; then
            :
        else
            continue
        fi

        if [ -f "${FILENAME}/.config/user-dirs.dirs" ]; then
            if [ ! $HOME ]; then
                HOME=${FILENAME}
            fi
            source "${FILENAME}/.config/user-dirs.dirs"
            if [ ! -d "${XDG_DESKTOP_DIR}" ]; then
                mkdir -p "${XDG_DESKTOP_DIR}" >/dev/null 2>&1 || true
            fi
            gDesktopName="${XDG_DESKTOP_DIR//${HOME}\//}"
        else
            if [ -d "${FILENAME}/桌面" ]; then
                gDesktopName="桌面"
            elif [ -d "${FILENAME}/Desktop" ]; then
                gDesktopName="Desktop"
            fi
        fi

        if [ -f  "${FILENAME}/${gDesktopName}/@@NAME@@.desktop" ]; then
			rm -f "${FILENAME}/${gDesktopName}/@@NAME@@.desktop"
		fi
    done
}
ky_prerm_uninstall_ide_desktop_icon
